//
//  RegimenLastViewController.m
//  味·道
//
//  Created by lanou3g on 15/7/1.
//  Copyright (c) 2015年 WMR. All rights reserved.
//

#import "RegimenLastViewController.h"
#import "MaocHeader.h"
#import "RequestManager.h"
#import "UMSocial.h"
@interface RegimenLastViewController ()<UIWebViewDelegate,UMSocialUIDelegate>
@property (nonatomic,retain)UIWebView *mainWebView;
@property (nonatomic,retain)UIActivityIndicatorView *activityIndicatorView;//缓冲图
@property (nonatomic,retain)UIButton *backButton;//返回上一页
@property (nonatomic,retain)UIButton *shareButton;//分享
@end

@implementation RegimenLastViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:0.6 green:0.2 blue:0.5 alpha:1];
    // Do any additional setup after loading the view.
    [self createMainWebView];
    [self createBackButton];
    [self createShareButton];
}

//创建UIWebView
-(void)createMainWebView{
    NSString *url = @"http://ibaby.ipadown.com/api/food/food.tips.detail.php?id=";
       url = [url stringByAppendingString:self.ID];
    UIWebView *webView = [[UIWebView alloc] init];
    webView.scrollView.backgroundColor = [UIColor whiteColor];
    webView.frame = CGRectMake(0, 60, SCREEN_WIDTH, SCREEN_HEIGHT-60);
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
    webView.delegate = self;
    self.mainWebView = webView;
    [self.mainWebView loadRequest:request];
    [self.view addSubview:self.mainWebView];
    [webView release];
}
//创建返回按钮
-(void)createBackButton{
    self.backButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
    [self.backButton setTitle:@"返回" forState:(UIControlStateNormal)];
    self.backButton.frame = CGRectMake(15, 30, 50, 18);
    [self.backButton addTarget:self action:@selector(backToFront:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:self.backButton];
}
//创建分享按钮
-(void)createShareButton{
    self.shareButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.shareButton.frame = CGRectMake(320, 30, 50, 18);
    [self.shareButton setTitle:@"分享" forState:(UIControlStateNormal)];
    [self.shareButton addTarget:self action:@selector(share:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:self.shareButton];
}
-(void)share:(UIButton *)sender{
    [UMSocialSnsService presentSnsIconSheetView:self appKey:@"559202ac67e58ed15c00167e"
                                      shareText:@"你要分享的文字" shareImage:[UIImage imageNamed:@"icon.png"]
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent,UMShareToRenren,UMShareToTwitter,nil] delegate:self];
}
-(void)backToFront:(UIButton *)sender{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - UIWebViewDelegate
-(void)webViewDidStartLoad:(UIWebView *)webView{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 60, SCREEN_WIDTH, SCREEN_HEIGHT-60)];
    [view setTag:180];
    [view setBackgroundColor:[UIColor blackColor]];
    [view setAlpha:0.5];
    [self.mainWebView addSubview:view];
    
    _activityIndicatorView = [[UIActivityIndicatorView alloc] init];
    [_activityIndicatorView setCenter:view.center];
    [view addSubview:_activityIndicatorView];
    [_activityIndicatorView startAnimating];
}
-(void)webViewDidFinishLoad:(UIWebView *)webView{
    [_activityIndicatorView stopAnimating];
    UIView *view = (UIView *)[self.mainWebView viewWithTag:180];
    [view removeFromSuperview];
}
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    [_activityIndicatorView stopAnimating];
    UIView *view = (UIView *)[self.mainWebView viewWithTag:180];
    [view removeFromSuperview];
}

-(void)requestError:(NSError *)error{
    NSLog(@"%@",error);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
